var clip = null;

copyInit();

function copyInit() {
	ZCBinder("sendUrl", "sendUrlBtn", "sendHistory", function () {
		$('#sendUrl').select();
		$("#sendMsg").html("已把转发地址复制到剪贴板!");
		$('#sendContent').css({
			height: "18px"
		});
	});
	ZCBinder("sendContent", "sendContentBtn", "sendHistory", function () {
		$('#sendContent').select().css({
			height: "200px"
		});
		$("#sendMsg").html("已把转发内容复制到剪贴板!");
	});
}
/* 转发查询结果 */
function openSenBox() {
	var shorturl;
	var frameName = $("#frameName").val();
	if(frameName == null) frameName = "";
	$("#sendMsg").html("");
	var url=encodeURIComponent("http://www.kuaidi100.com/chaxun?from=" + frameName + "&com=" + gCompanyCode + "&nu=" + gKuaidiNumber);
	var longurl="//www.kuaidi100.com/buildshortlink?m=build&url="+url;
	$.get(longurl,function(resultData){
		shorturl=resultData;
		$("#sendUrl").val(shorturl);
		$("#sendContent").val(gCompanyName + "单号“" + gKuaidiNumber + "”的查询结果：\n......\n" + translateTotext(gResultData, 2) + "更多详情："+shorturl);
	})
	$('#sendHistoryBtn').hide();
	$('#sendHistory').show();
	$('#weixin').hide();
	$('#sendUrl').blur();
	$('#sendContent').blur();
	$("#selectedTag").removeClass("pos1").addClass("pos2").show();
}

//转发最新记录
function translateTotext(resultData, len) {
	try {
		if (len > resultData.length) len = resultData.length;
		var html = "";
		for (var i = len - 1; i >= 0; i--) {
			html += $.trim(resultData[i].time) + " " + $.trim(resultData[i].context).replace(/\s+/g, ' ') + "\n";
		}
		return html;
	} catch (e) {}
}

function ZCBinder(inputId, btnId, boxId, callback) {
	clip = new ZeroClipboard.Client();
	ZeroClipboard.setMoviePath("//cdn.kuaidi100.com/images/ZeroClipboard.swf");
	clip.setHandCursor(true);
	$("#" + btnId).mouseover(function () {
		clip.setText($("#" + inputId).val());
		if (clip.div) {
			clip.reposition(btnId);
		} else clip.glue(btnId);
		clip.addEventListener('complete', function () {
			if (callback != null) {
				callback();
			}
		});
		clip.receiveEvent('mouseover', null);
	});
}

//取消选择文本控制
function selectCtrl() {
	if (document.selection) {
		document.selection.empty();
	} else if (window.getSelection) {
		window.getSelection().removeAllRanges();
	}
	$('.sendMsg').html('');
};

function closeHisCtrl() {
	selectCtrl();
	$('.send-box').hide();
	$('.send-box-on').show();
	$('.sendMsg').html('');
	$('#selectedTag').hide();
	$('#sendContent').css({
		height: "18px"
	});
}